<?php
ini_set('date.timezone','Asia/Shanghai');//调整时区
session_start();
$robot=$_SESSION['robot'];
$s_time=$_SESSION['s_time'];
$e_time=$_SESSION['e_time'];
$time=$_SESSION['time'];
$table=$_SESSION['table'];
//$robot1="ap4";
//$robot2="ap4x";
//$s_time="2017-04-07 08:00";
//$e_time="2017-04-07 12:00";
//$time=1;
$data=array();
$f=floor((strtotime($e_time)-strtotime($s_time))/86400);
$f=floor((strtotime($e_time)-strtotime($s_time))%86400/3600)+$f*24;
if($time==1){
    $num=13;
}else if($time==10){
    $num=15;
}
/*连接数据库*/
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "data";

$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
}
//按群组分，入群人数和图片分享率


$array = explode(",",$robot);
$robot_where="hdid = '$array[0]'";
for($index=1;$index<count($array);$index++)
{
    $robot_where=$robot_where."or hdid = '$array[$index]'";
}
//$sql="SELECT * , COUNT( * ) AS count, SUBSTRING(  `date` , 1, 15 ) AS dateindex FROM msg WHERE mtype =10000 and (hdid = '$hdid1'or hdid = '$hdid2') GROUP BY user_id HAVING  SUBSTRING(  `date` , 1, '$num' )='$time' order by user_name asc";
$sql=" SELECT * , COUNT( * ) AS count, SUBSTRING(  `date` , 1, '$num' ) AS dateindex FROM msg WHERE mtype =10000 and (".$robot_where.") and( date BETWEEN '$s_time'and '$e_time') GROUP BY user_name order by user_name asc";

//echo $sql."<br/>";
$result = $conn->query($sql);//查询到的结果集
$total=array();//放置查询的数据
if ($result->num_rows > 0) {
    // 输出每行数据
    $i=0;
    while($row = $result->fetch_assoc()) {
            $name=$row['user_name'];//群组的名称
            $user[$name]=$i;
          //  $user[$i]=$i;
            $sql1="SELECT *  FROM msg WHERE mtype =3 and user_name='$name' and (".$robot_where.") and( date BETWEEN '$s_time'and '$e_time')";//在群里筛出图片分享数
            $res = $conn->query($sql1);
            $count[$name]=$row['count'];
            $i_count[$name]=$res->num_rows;
            $value[$name]=round($i_count[$name]/$count[$name]*100,2);//计算出分享率
            $i++;
           // $total[]=$data;
       // }
    }
} else{
    echo "没有数据";
}
//$data1[]=$result->num_rows;
$data['user']=$user;
$data['count']=$count;
$data['i_count']=$i_count;
$data['value']=$value;
echo json_encode($data);
$conn->close();

?>